#include "ZProcessorMgr.h"
#include "ZRecvProcessor.h"
#include "ZSendProcessor.h"

ZProcessorMgr::ZProcessorMgr()
{
	m_pRecvProcessor = NULL;
	m_pSendProcessor = NULL;
}

ZProcessorMgr::~ZProcessorMgr()
{
	delete m_pRecvProcessor;
	delete m_pSendProcessor;
}

void ZProcessorMgr::SendTo( const QHostAddress& addr, quint16 port, const ZPackage& pack )
{
	m_pSendProcessor->SentTo(addr, port, pack);
}

void ZProcessorMgr::RegReactor(ZPackReactor* pReactor)
{
	m_reactorMgr.RegReactor(pReactor);
}

bool ZProcessorMgr::Init( int iBindPort /*= -1*/ )
{
	if (iBindPort == -1)
	{
		quint16 iBindPort = ZUdpDefPort+10;

		while(true)
		{
			if(m_udpSocket.bind(iBindPort, QUdpSocket::ShareAddress))
				break;
			iBindPort++;
		}
	}
	else
	{
		if (!m_udpSocket.bind(iBindPort, QUdpSocket::ShareAddress))
		{
			return false;
		}
	}

	m_pRecvProcessor = new ZRecvProcessor(&m_udpSocket, this);
	m_pRecvProcessor->start();
	m_pSendProcessor = new ZSendProcessor(&m_udpSocket, this);
	m_pSendProcessor->start();

	return true;
}
